package com.example.liwushuo.ui;


import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.liwushuo.R;
import com.example.liwushuo.fragment.HomeFragment;
import com.example.liwushuo.fragment.HotFragment;
import com.example.liwushuo.fragment.KindFragment;
import com.example.liwushuo.fragment.MyFragment;
import com.example.liwushuo.utils.FragmentTabUtils;

import java.util.ArrayList;

import static android.graphics.Color.WHITE;

public class MainActivity extends BaseActivity implements FragmentTabUtils.OnRgsExtraCheckedChangedListener {

    private RadioGroup rg;
    private ArrayList<Fragment> fragments;
    private Button btn;
    private PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initData() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new HotFragment());
        fragments.add(new KindFragment());
        fragments.add(new MyFragment());
        new FragmentTabUtils(getSupportFragmentManager(),fragments,R.id.main_fragment_container,rg,this);
    }

    private void initView() {
        rg = (RadioGroup) findViewById(R.id.main_rg);
        btn = (Button) findViewById(R.id.btn);

        final View view=getLayoutInflater().inflate(R.layout.iem_pop_layout, null);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT,false);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow.isShowing()){
                    popupWindow.dismiss();
                }else {
                    popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
                }
            }
        });

    }

    @Override
    public void OnRgsExtraCheckedChanged(RadioGroup radioGroup, int checkedId, int index) {
        switch (index){
            case 0:
                btn.setVisibility(View.VISIBLE);
                break;
            default:
                btn.setVisibility(View.GONE);
                break;
        }
    }
}
